// 封装请求
const baseUrl = "http://localhost:8080";
const request = (options) => {
  const url = options.url.startsWith("http") ? options.url : `${baseUrl}${options.url}`
  const defaultHeader = {
    "content-type": 'application/json',
    "Authorization": `${wx.getStorageSync('token')||''}`
  }
  const header = {
    ...defaultHeader,
    ...options.header
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: options.method || "GET",
      data: options.data || {},
      header: header,
      success: res => {
        if (res.statusCode === 200) {
          resolve(res.data)
        } else {
          wx.showToast({
            title: '请求错误',
            icon: 'error',
            duration: 1500
          })
          reject(res)
        }
      },
      fail: err => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'error',
          duration: 1500
        })
        reject(err)
      }
    })
  })
}

export default request;